﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Xml;
using System.Windows.Forms;
using System.Data;
namespace CustomXML
{

public static class XMLData
{

    private static string ConfigName = @"config.xml";

    //DataTransaction
    private static DataSet XmlDs = new DataSet("MAC&INFOMATION");
    private static DataTable MacDt = new DataTable("MAC");
    private static DataTable InfoDt = new DataTable("INFOMATION");
    /// <summary>
    /// 构造函数初始化
    /// </summary>
    static XMLData()
    {

        try
        {
        XmlDs.ReadXml(ConfigName);
        //对两个表进行读取值赋值

            MacDt = XmlDs.Tables[0];
            InfoDt = XmlDs.Tables[1];
            XmlDs.Tables.Add(MacDt);
            XmlDs.Tables.Add(InfoDt);
        }
        catch (System.Exception e)
        {//如果读取值失败,证明表不存在或者表信息不完全,则进行以下信息初始化
            XmlDs.Tables.Clear();
            MacDt.Clear();
            InfoDt.Clear(); 
            DataColumn dc = new DataColumn("MACAddress", Type.GetType("System.String"));
            DataColumn infodc_event = new DataColumn("Event", Type.GetType("System.String"));
            DataColumn infodc_value = new DataColumn("Value", Type.GetType("System.String"));
            InfoDt.Columns.Add(infodc_event);
            InfoDt.Columns.Add(infodc_value);
            MacDt.Columns.Add(dc);

        }
            
    }
    /// <summary>
    /// 保存项目
    /// </summary>
    /// <param name="location">所要保存的栏目的名字</param>
    /// <param name="value">所要保存的栏目的值</param>
    public static void InsertEvent(string location, string value)
    {
        DataRow dr = InfoDt.NewRow();
        dr["Event"] = location;
        dr["Value"] = value;
        InfoDt.Rows.Add(dr);
    }

    /// <summary>
    /// 查询相对应的栏目的值
    /// </summary>
    /// <param name="location">所要查询的栏目的名字</param>
    /// <returns>如果成功返回对应的string,失败则返回null</returns>
    public static string InquireEvent(string location)
    {
        try
        {
            for (int i = 0; i < InfoDt.Rows.Count - 1; i++)
            {

                if (location == InfoDt.Rows[i][0].ToString())
                {
                    return InfoDt.Rows[i][1].ToString();
                }
            }
        }
        catch (System.Exception e)
        {
            return null;
        }
        return null;
    }

    /// <summary>
    /// 删除栏目以及对应的值
    /// </summary>
    /// <param name="location">所要删除的栏目的名字</param>
    /// <returns>成功返回true,否则为false</returns>
    public static bool DeleteEvent(string location)
    {
        try
        {
            for (int i = 0; i < InfoDt.Rows.Count - 1; i++)
            {

                if (location == InfoDt.Rows[i][0].ToString())
                {
                    InfoDt.Rows.RemoveAt(i);
                    return true;
                }
            }
        }
        catch (System.Exception e)
        {
            return false;
        }
        return false;
    }

    /// <summary>
    /// 修改栏目的值
    /// </summary>
    /// <param name="location">所要修改的栏目</param>
    /// <returns>成功返回true,失败返回false</returns>
    public static bool ModifyEvent(string location)
    {
        try
        {
            for (int i = 0; i < InfoDt.Rows.Count - 1; i++)
            {

                if (location == InfoDt.Rows[i][0].ToString())
                {
                    InfoDt.Rows[i][1] = location;
                    return true;
                }
            }
        }
        catch (System.Exception e)
        {
            return false;
        }
        return false;
    }

    public static void ClearMAC()
    {
        MacDt.Rows.Clear();
    }
    public static void InsertMAC(string MacAddress)
    {
        DataRow dr = MacDt.NewRow();
        dr["MACAddress"] = MacAddress;
        MacDt.Rows.Add(dr);
    }
    public static string ReadMAC(int index)
    {
        try
        {
            return Convert.ToString(MacDt.Rows[index][0]);
        }
        catch (System.Exception e)
        {
            return null;
        }
         
    }

    /// <summary>
    /// 保存当前所有的数据到XML,在对信息操作完以后一定要调用该函数,否则不能保存,对MAC地址会自动保存
    /// </summary>
    public static void SaveTable()
    {
        XmlDs.Tables.Clear();
        XmlDs.Tables.Add(MacDt);
        XmlDs.Tables.Add(InfoDt);
        XmlDs.WriteXml(ConfigName);
    }
}



/// <summary>
/// 用来维护保存MAC地址列表,如下为范例代码
/*           List<string> macaddress = new List<string>();
            for (int i = 0; i < 10; i++)
                macaddress.Add(i.ToString());
            MacListManagement.WriteMacToStore(macaddress);

            List<string> macaddr = new List<string>();
            macaddr = MacListManagement.ReadStoredMac();
            foreach(string a in macaddr)
            {
                MessageBox.Show(a);
            }
 */ 
/// </summary>
public static class MacListManagement
{
    private static List<string> MacStoreList = new List<string>();
    
    /// <summary>
    /// 返回当前系统已经储存的MAC地址
    /// </summary>
    /// <returns>List#string#</returns>
    public static List<string> ReadStoredMac()
    {
        string macstr = null;
        int MacIndex = 0;

        do
        {
            macstr = XMLData.ReadMAC(MacIndex);
            if (macstr != null)
            {
                MacStoreList.Add(macstr);
                MacIndex++;
            }
            else break;
        }
        while (macstr != null);

        return MacStoreList;
    }

    /// <summary>
    /// 写入MAC
    /// </summary>
    /// <param name="inputMac">待要保存的MAC地址列表</param>
    public static void WriteMacToStore(List<string> inputMac)
    {
        XMLData.ClearMAC();
        foreach(string mac in inputMac)
        {
            XMLData.InsertMAC(mac);
        }
        XMLData.SaveTable();
    }
    
}
}